# Current, as of Thursday, May 28, 2020.
# rm(list = ls())
# setwd("~/Dropbox/Base Dropbox/List Experiment Paper (PS)/Conditional Accept/Replication Files (Carmines-Schmidt PS)/2008 CCAP/ANOVA Tests")
library(foreign)
library(car)
library(readstata13)
library(zeligverse)
library(ggplot2)
library(stargazer)
library(apsrtable)
library(plyr)
#-----
# Input the data.
our.data = read.dta13("CarminesSchmidtPS-replication-2008.dta", generate.factors = TRUE)
names(ccap.2008)
#-----
# CCAP 2008, preparing data for ANOVA tests. 

# Subset the data to remove the individuals NOT assigned to one of the treatment conditions.
ccap.2008 = subset(ccap.2008, treatment!="")
table(ccap.2008$treatment)
# Includes only respondents assigned to the "African American candidate," "Control," 
# "Gay or Homosexual candidate," "Muslim candidate," or "Woman candidate" LEs. 

# Indicator variable: assignment to ANY of the sensitive-item conditions. 
ccap.2008$sensitive.item = ifelse(ccap.2008$treatment!="Control", "Sensitive-Item", "Control")
table(ccap.2008$sensitive.item)
table(ccap.2008$sensitive.item, ccap.2008$treatment)
# Dichotomous, numeric version of this variable (1 = sensitive-item conditions; 0 = treatment condition).
ccap.2008$sensitive.item.binary = ifelse(ccap.2008$sensitive.item=="Sensitive-Item", 1, 0) 
table(ccap.2008$sensitive.item.binary)
table(ccap.2008$sensitive.item.binary, ccap.2008$treatment)
# Factor variable.
ccap.2008$sensitive.item.factor = as.factor(ccap.2008$sensitive.item.binary)
table(ccap.2008$sensitive.item.factor)
#-----
# Divide the dataset into four subsamples: each with the baseline condition and ONE of the sensitive-item conditions.
# The 'African American' and baseline LE subsets should be just non-African-American respondents. 

# 'African American' and baseline
afroam.experiment = subset(ccap.2008, treatment=="African Americans" | treatment=="Control")
afroam.experiment = subset(afroam.experiment, black!=1)
table(afroam.experiment$treatment) 

# 'Gay or Homosexual' and baseline
gays.experiment = subset(ccap.2008, treatment=="Gays" | treatment=="Control")
table(gays.experiment$treatment)

# 'Muslim' and baseline
muslims.experiment = subset(ccap.2008, treatment=="Muslims" | treatment=="Control")
table(muslims.experiment$treatment)

# 'Woman' and baseline 
women.experiment = subset(ccap.2008, treatment=="Women" | treatment=="Control")
table(women.experiment$treatment)
#-----
# To run the ANOVA tests, make assignment-to-treatment a factor variable, for each of the four datasets.

# Treatment (factor), "African American" and baseline
afroam.experiment$treatment.factor = as.factor(afroam.experiment$treatment)
table(afroam.experiment$treatment.factor)
levels(afroam.experiment$treatment.factor)

# Treatment (factor), "Gay or Homosexual" and baseline
gays.experiment$treatment.factor = as.factor(gays.experiment$treatment)
table(gays.experiment$treatment.factor)
levels(gays.experiment$treatment.factor)

# Treatment (factor), "Muslim" and baseline
muslims.experiment$treatment.factor = as.factor(muslims.experiment$treatment)
table(muslims.experiment$treatment.factor)
levels(muslims.experiment$treatment.factor)

# Treatment (factor), "Female" and baseline
women.experiment$treatment.factor = as.factor(women.experiment$treatment)
table(women.experiment$treatment.factor)
levels(women.experiment$treatment.factor)
#-----
# ANOVA tests for balance: AFRICAN AMERICAN and BASELINE conditions (excluding African American respondents)

# 'African American' and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ treatment.factor, data = afroam.experiment)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ treatment.factor, data = afroam.experiment)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ treatment.factor, data = afroam.experiment)
summary(gender.fit)
# White
white.fit = aov(white ~ treatment.factor, data = afroam.experiment)
summary(white.fit)
# Education
education.fit = aov(education ~ treatment.factor, data = afroam.experiment)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ treatment.factor, data = afroam.experiment)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ treatment.factor, data = afroam.experiment)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ treatment.factor, data = afroam.experiment)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ treatment.factor, data = afroam.experiment)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ treatment.factor, data = afroam.experiment)
summary(income.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means.afroam = ddply(afroam.experiment, .(treatment.factor), summarize, 
                         mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                         mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                         mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                         mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                         mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                         mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                         mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                         mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                         mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                         mean.income = round(mean(income, na.rm=TRUE), digits = 2))
our.means.afroam
#-----
# ANOVA tests for balance: GAY-OR-HOMOSEXUAL and BASELINE conditions

# 'Gay or Homosexual' and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ treatment.factor, data = gays.experiment)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ treatment.factor, data = gays.experiment)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ treatment.factor, data = gays.experiment)
summary(gender.fit)
# Black
black.fit = aov(black ~ treatment.factor, data = gays.experiment)
summary(black.fit)
# White
white.fit = aov(white ~ treatment.factor, data = gays.experiment)
summary(white.fit)
# Education
education.fit = aov(education ~ treatment.factor, data = gays.experiment)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ treatment.factor, data = gays.experiment)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ treatment.factor, data = gays.experiment)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ treatment.factor, data = gays.experiment)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ treatment.factor, data = gays.experiment)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ treatment.factor, data = gays.experiment)
summary(income.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means.gays = ddply(gays.experiment, .(treatment.factor), summarize, 
                       mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                       mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                       mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                       mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                       mean.black = round(mean(black, na.rm=TRUE), digits = 2),
                       mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                       mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                       mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                       mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                       mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                       mean.income = round(mean(income, na.rm=TRUE), digits = 2))
our.means.gays
#-----
# ANOVA tests for balance: MUSLIM and BASELINE conditions

# 'Muslim' and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ treatment.factor, data = muslims.experiment)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ treatment.factor, data = muslims.experiment)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ treatment.factor, data = muslims.experiment)
summary(gender.fit)
# Black
black.fit = aov(black ~ treatment.factor, data = muslims.experiment)
summary(black.fit)
# White
white.fit = aov(white ~ treatment.factor, data = muslims.experiment)
summary(white.fit)
# Education
education.fit = aov(education ~ treatment.factor, data = muslims.experiment)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ treatment.factor, data = muslims.experiment)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ treatment.factor, data = muslims.experiment)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ treatment.factor, data = muslims.experiment)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ treatment.factor, data = muslims.experiment)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ treatment.factor, data = muslims.experiment)
summary(income.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means.muslims = ddply(muslims.experiment, .(treatment.factor), summarize, 
                          mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                          mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                          mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                          mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                          mean.black = round(mean(black, na.rm=TRUE), digits = 2),
                          mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                          mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                          mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                          mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                          mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                          mean.income = round(mean(income, na.rm=TRUE), digits = 2))
our.means.muslims
#-----
# ANOVA tests for balance: WOMAN and BASELINE conditions

# 'Woman' and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ treatment.factor, data = women.experiment)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ treatment.factor, data = women.experiment)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ treatment.factor, data = women.experiment)
summary(gender.fit)
# Black
black.fit = aov(black ~ treatment.factor, data = women.experiment)
summary(black.fit)
# White
white.fit = aov(white ~ treatment.factor, data = women.experiment)
summary(white.fit)
# Education
education.fit = aov(education ~ treatment.factor, data = women.experiment)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ treatment.factor, data = women.experiment)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ treatment.factor, data = women.experiment)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ treatment.factor, data = women.experiment)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ treatment.factor, data = women.experiment)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ treatment.factor, data = women.experiment)
summary(income.fit)
# Clinton favorability
clinton.fit = aov(clinton_favor ~ treatment.factor, data = women.experiment)
summary(clinton.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means.female = ddply(women.experiment, .(treatment.factor), summarize, 
                         mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                         mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                         mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                         mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                         mean.black = round(mean(black, na.rm=TRUE), digits = 2),
                         mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                         mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                         mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                         mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                         mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                         mean.income = round(mean(income, na.rm=TRUE), digits = 2),
                         mean.clinton = round(mean(clinton_favor, na.rm = TRUE), digits = 2))
our.means.female
#-----
# ANOVA tests for balance: SENSITIVE-ITEM CONDITIONS (pooled) and BASELINE conditions

# Pooled sensitive-item conditions and baseline LEs
# Partisanship 
pid.fit = aov(pid_7 ~ sensitive.item.factor, data = ccap.2008)
summary(pid.fit)
# Ideology
ideology.fit = aov(ideology ~ sensitive.item.factor, data = ccap.2008)
summary(ideology.fit)
# Gender
gender.fit = aov(female ~ sensitive.item.factor, data = ccap.2008)
summary(gender.fit)
# Black
black.fit = aov(black ~ sensitive.item.factor, data = ccap.2008)
summary(black.fit)
# White
white.fit = aov(white ~ sensitive.item.factor, data = ccap.2008)
summary(white.fit)
# Education
education.fit = aov(education ~ sensitive.item.factor, data = ccap.2008)
summary(education.fit)
# Church attendance
church.fit = aov(church ~ sensitive.item.factor, data = ccap.2008)
summary(church.fit)
# Racial resentment
rr.fit = aov(rr ~ sensitive.item.factor, data = ccap.2008)
summary(rr.fit)
# Obama favorability
obama.fit = aov(obama_favor ~ sensitive.item.factor, data = ccap.2008)
summary(obama.fit)
# Political interest
interest.fit = aov(interest ~ sensitive.item.factor, data = ccap.2008)
summary(interest.fit)
# Family income 
income.fit = aov(income ~ sensitive.item.factor, data = ccap.2008)
summary(income.fit)
# Clinton favorability 
clinton.fit = aov(clinton_favor ~ sensitive.item.factor, data = ccap.2008)
summary(clinton.fit)

# Use the 'plyr' package to take the means of these different variables across the conditions.
# This simply helps concatenate the output into a meaningful table.
our.means = ddply(ccap.2008, .(sensitive.item.factor), summarize, 
                  mean.pid = round(mean(pid_7, na.rm=TRUE), digits = 2),
                  mean.ideology = round(mean(ideology, na.rm=TRUE), digits = 2),
                  mean.female = round(mean(female, na.rm=TRUE), digits = 2),
                  mean.white = round(mean(white, na.rm=TRUE), digits = 2),
                  mean.black = round(mean(black, na.rm=TRUE), digits = 2),
                  mean.education = round(mean(education, na.rm=TRUE), digits = 2),
                  mean.church = round(mean(church, na.rm=TRUE), digits = 2),
                  mean.rr = round(mean(rr, na.rm=TRUE), digits = 2),
                  mean.obama = round(mean(obama_favor, na.rm=TRUE), digits = 2),
                  mean.interest = round(mean(interest, na.rm=TRUE), digits = 2),
                  mean.income = round(mean(income, na.rm=TRUE), digits = 2),
                  mean.clinton = round(mean(clinton_favor, na.rm=TRUE), digits = 2))
our.means

